//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.SysMessage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class SysMessageDao extends BaseDao<SysMessage, Integer> {
	public SysMessageDao() {
	}

	public int getNewMsgNum(String userCode, String shopCode) {
		String sql = "select count(*)\nfrom SysMessage m left join SysMessageRead mr on m.msgId=mr.msgId and mr.userCode=:p1\nwhere m.status>0 and m.sendTime>dateadd(day,-3, getdate()) and mr.readTime is null\nand (m.recvCode like \'%0-all%\' or m.recvCode like :p2)";
		return ((Integer)this.getBySql(Integer.class, sql, new Object[]{userCode, "%3-" + shopCode + "%"})).intValue();
	}

	public void queryReadPageList(Pagination<SysMessage> pg, String userCode, List<String> recvList) {
		ArrayList params = new ArrayList();
		params.add(userCode);
		ConditionMap cd = new ConditionMap(1);
		Iterator sqlw = recvList.iterator();

		String sql;
		while(sqlw.hasNext()) {
			sql = (String)sqlw.next();
			cd.like("m.recvCode", "%" + sql + "%");
		}

		String sqlw1 = cd.toSql(params);
		sql = "select m.msgId, title, sendTime, case when mr.readTime is null then 0 else 1 end status, createUser, createTime, updateTime\nfrom SysMessage m left join SysMessageRead mr on m.msgId=mr.msgId and mr.userCode=:p1\nwhere m.status>0 and (mr.deleteLabel=0 or mr.deleteLabel is null)\n" + (sqlw1.length() > 0?" and (" + sqlw1 + ")":"") + "order by status, m.sendTime desc";
		this.queryPageListBySql(SysMessage.class, pg, sql, params.toArray());
	}

	public boolean updateStatus(int msgId, int status) {
		String hql = "update SysMessage set status=:p1 where msgId=:p2";
		return this.updateByHql(hql, new Object[]{Integer.valueOf(status), Integer.valueOf(msgId)}) > 0;
	}
}
